package com.kamkor.interviewer.client.view;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Label;
import com.kamkor.interviewer.client.presenter.AnswerInterviewPresenter.Display;
import com.kamkor.interviewer.shared.QuestionAnswerDetails;

/**
 * 
 * @author kamkor
 *
 */
public class AnswerInterviewView extends Composite implements Display {
	interface MyUiBinder extends UiBinder<Widget, AnswerInterviewView> {}
	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);	
	
	@UiField
	FlexTable questionAnswerTable;
	
	@UiField 
	Button saveAll;
	
	@UiField	
	CheckBox autoSave;
	
	@UiField 
	Label isSavedInfo;	
	
	ChangeHandler answerTextChangeHandler;
	
	public AnswerInterviewView() {
		initWidget(uiBinder.createAndBindUi(this));	
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public FlexTable getQuestionAnswerTable() {
		return questionAnswerTable;
	}
	
	@Override
	public List<QuestionAnswerDetails> getData(Long interviewId) {
		final int rowCount = questionAnswerTable.getRowCount();
		List<QuestionAnswerDetails> list = new ArrayList<QuestionAnswerDetails>(rowCount);

		for(int i = 0; i < rowCount; i++) {			
			DecoratorPanel decPanel = (DecoratorPanel) questionAnswerTable.getWidget(i, 0);
			VerticalPanel panel = (VerticalPanel) decPanel.getWidget();		
			Label questionId = (Label) panel.getWidget(0);
			Label questionPosition = (Label) panel.getWidget(1);
			// 2 is questionLabel
			Label question = (Label) panel.getWidget(3);
			TextArea answer = (TextArea) panel.getWidget(4);				
						
			QuestionAnswerDetails qaDetails = new QuestionAnswerDetails(
					new Long(questionId.getText()), interviewId, question.getText(), answer.getText());
			
			qaDetails.setPositionNumber(new Long(questionPosition.getText()));
			list.add(qaDetails);
		}	
		return list;
	}

	@Override
	public void setData(List<QuestionAnswerDetails> list) {
		for(int row = 0; row < list.size(); row++) {
			QuestionAnswerDetails questionAnswer = list.get(row);
			addToData(questionAnswer);
		}
	}

	@Override
	public void addToData(QuestionAnswerDetails questionAnswer) {
		// invisible label that contains question id
		Label questionId = new Label(questionAnswer.getId().toString());
		questionId.setVisible(false);
		
		// invisible label that contains question position
		Label questionPosition = new Label(questionAnswer.getPositionNumber().toString());
		questionPosition.setVisible(false);	
		
		final int newRow = questionAnswerTable.getRowCount();
		Label questionLabel = new Label(new String("Pytanie nr " + newRow));
		questionLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);		
		questionLabel.setStyleName("questionAnswerHeader");
		questionLabel.setVisible(false);		
	
		Label question = new Label(questionAnswer.getQuestion());
		question.setText(questionAnswer.getQuestion());
		question.setStyleName("questionLabel");
		final TextArea answer = new TextArea();
		answer.setText(questionAnswer.getAnswer());	
		answer.setStyleName("gwt-RichTextArea-answer2");	
		answer.addChangeHandler(answerTextChangeHandler);		
				
		VerticalPanel panel = new VerticalPanel();
		panel.add(questionId);
		panel.add(questionPosition);
		panel.add(questionLabel);
		panel.add(question);
		panel.add(answer);
		panel.setStyleName("questionAnswer-panel");
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(panel);		
		questionAnswerTable.setWidget(newRow, 0, decPanel);						
	}

	@Override
	public HasClickHandlers getSaveAll() {
		return saveAll;		
	}

	@Override
	public Label getIsSavedInfo() {
		return isSavedInfo;
	}

	@Override
	public void setAnswerTextChangeHandler(ChangeHandler handler) {
		this.answerTextChangeHandler = handler;
	}
	
	public boolean isAutoSave() {
		return autoSave.getValue();
	}
}
